<?php 
	class Learn_cards extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	function get_learn_card_list($limit = 0, $offset = 10, $conditions = array()){
    		$where = "user_id = '{$this->user_data['id']}' AND is_activation = 1";
    		if(!empty($conditions['card_num'])){
    			$where .= " AND card_num LIKE '%{$conditions['card_num']}%'";
    		}
    		if(!empty($conditions['title'])){
                $where .= " AND title LIKE '%{$conditions['title']}%'";
            }
            if(!empty($conditions['activation_time'])){
                $activation_time = strtotime(date("Y-m-d 23:59:59",strtotime($conditions['activation_time'])));
                $where .= " AND activation_time + 3600 * 24 * expire_day <= '{$activation_time}'";
            }
    		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}o2o_school_learn_card WHERE $where ";
    		$sql .= "ORDER BY activation_time DESC LIMIT $limit, $offset ";
    		$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("total_count" => $row_num['row_num'], "result" => $result);
    	}

        function get_learn_item($card_id){
            $sql = "SELECT item.*,s.title,c.title AS course_title FROM {$this->db->dbprefix}o2o_school_learn_card_item AS item ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}course AS c ON(c.id=item.content_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=item.content_id) ";
            $sql .= "WHERE card_id = '{$card_id}' ";
            return $result = $this->db->query($sql)->result_array();
        }
    }